﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace CoolCode.Controls
{
    public class Window
    {
        private static Loading loading;
        private readonly static string loadingName = "loading";

        private static Panel dialogGrid;
        private readonly static string dialogGridName = "dialogGrid";

        private readonly static string commonDialog = "commonDialog";
        static Window()
        {
            if (loading == null)
            {
                UserControl rootPage = Application.Current.RootVisual as UserControl;
                if (rootPage != null)
                {
                    loading = rootPage.FindName(loadingName) as Loading;
                }
            }

            if (dialogGrid == null)
            {
                UserControl rootPage = Application.Current.RootVisual as UserControl;
                if (rootPage != null)
                {
                    dialogGrid = rootPage.FindName(dialogGridName) as Panel;
                }
            }
        }

        public static void ShowLoading()
        {
            ShowLoading("");
        }

        public static void ShowLoading(string message)
        {
            if (loading != null)
            {
                loading.Dispatcher.BeginInvoke(() =>
                {
                    loading.Visibility = Visibility.Visible;
                    loading.SetLoadingText(message);
                });
            }
        }

        public static void HideLoading()
        {
            if (loading != null)
                loading.Dispatcher.BeginInvoke(() => loading.Visibility = Visibility.Collapsed);
        }

        public static void Alert(string msg)
        {
            if (dialogGrid != null)
                dialogGrid.Dispatcher.BeginInvoke(() =>
                {
                    dialogGrid.Children.Clear();
                    dialogGrid.Children.Add(new Alert(msg));
                });
        }

        public static void Confirm(string msg, OnConfirm onConfirm)
        {
            if (dialogGrid != null)
                dialogGrid.Dispatcher.BeginInvoke(() =>
                {
                    dialogGrid.Children.Clear();
                    dialogGrid.Children.Add(new Confirm(msg,onConfirm));
                });
        }

        public static void ShowDialog(Panel panel, UserControl dialog)
        {
            Border placeHolder = panel.FindName(commonDialog) as Border;
            if (placeHolder == null)
            {
                placeHolder = new Border
                {
                    Name = commonDialog,
                    VerticalAlignment = VerticalAlignment.Stretch,
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    Background = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100)),
                    Opacity = 0.7
                };
                panel.Children.Add(placeHolder);
            }
            placeHolder.Child = dialog;
            placeHolder.Visibility = Visibility.Visible;

        }

        public static void CloseDialog()
        {
            UserControl rootPage = Application.Current.RootVisual as UserControl;
            Border placeHolder = rootPage.FindName(commonDialog) as Border;
            if (placeHolder != null)
            {
                placeHolder.Child = null;
                placeHolder.Visibility = Visibility.Collapsed;
            }
        }

        public static void ShowOpPanel(string title, IDialog panel)
        {
            if (dialogGrid != null)
                dialogGrid.Dispatcher.BeginInvoke(() =>
                {
                    dialogGrid.Children.Clear();
                    dialogGrid.Children.Add(new Dialog(title, panel));
                });
        }
        public static void Show2(UIElement dialog)
        {
            Popup popup = new Popup();
            Border placeHolder = new Border();
            UserControl rootPage = Application.Current.RootVisual as UserControl;
            double w = rootPage.ActualWidth;
            double h = rootPage.ActualHeight;
            placeHolder = new Border
            {
                Name = commonDialog,
                Background = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100)),
                Opacity = 0.7,
                Width = w,
                Height = h,
            };

            DialogPanel d = new DialogPanel
            {
                Height = 100,
                Width = 300,
                Title = "Dialog",
                DragEnable = true,

            };
            d.Content =dialog 
            placeHolder.Child = dialog;
            placeHolder.Visibility = Visibility.Visible;
            popup.Child = placeHolder;

            popup.IsOpen = true;
        }

        public static void Show(UIElement dialog)
        {
            Popup popup = new Popup();
            Border placeHolder = new Border();
            UserControl rootPage = Application.Current.RootVisual as UserControl;
            double w = rootPage.ActualWidth;
            double h = rootPage.ActualHeight;
                placeHolder = new Border
                {
                    Name = commonDialog,
                    Background = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100)),
                    Opacity = 0.7,
                    Width =w,
                    Height=h,
                };
           
            placeHolder.Child = dialog;
            placeHolder.Visibility = Visibility.Visible;
                popup.Child=placeHolder;
             
            popup.IsOpen = true;
    }

    }
}